<template>
  <j-modal
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    @ok="handleOk"
    width="40%"
    :confirmLoading="confirmLoading"
    :okText="$t('保存')"
    :cancelText="$t('关闭')"
    v-drag
  >
    <div>
      <a-form-model
        :rules="rules"
        :model="dataForm"
        ref="form"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        v-enterToNext
      >
        <a-form-model-item prop="ip" ref="ip" :label="$t('ip')">
          <a-input v-model="dataForm.ip"></a-input>
        </a-form-model-item>
        <a-form-model-item prop="userName" ref="userName" :label="$t('用户名')">
          <a-input v-model="dataForm.userName"></a-input>
        </a-form-model-item>
        <a-form-model-item prop="password" ref="password" :label="$t('密码')">
          <a-input v-model="dataForm.password"></a-input>
        </a-form-model-item>
        <a-form-model-item prop="deviceId" ref="deviceId" :label="$t('c_吧台')">
          <j-dict-select-tag type="list" v-model="dataForm.deviceId" dictCode="bis_device_info where type='EF000000',name,id" :placeholder="$t('请选择')"/>
        </a-form-model-item>
      </a-form-model>
    </div>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
export default {
  name: 'BbpayModal',
  components: {},
  data() {
    return {
      model: {},
      title: '',
      visible: false,
      confirmLoading: false,
      dataForm: {
        ip: '',
        userName: '',
        password: '',
      },
      labelCol: {
        xs: { span: 20 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 18 },
      },
      rules: {
        ip: [{ required: true, message: this.$t('请输入') }],
        userName: [{ required: true, message: this.$t('请输入') }],
        password: [{ required: true, message: this.$t('请输入') }],
        deviceId: [{ required: true, message: this.$t('请选择') }]
      },
      url: {
        add: '/pay/bisPayChannelBBPay/add',
        edit: '/pay/bisPayChannelBBPay/edit',
      },
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.dataForm))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      let that = this
      that.visible = true
      that.dataForm = Object.assign({}, record)
    },
    handleOk() {
      let that = this
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let url = ''
          let method = ''
          if (that.dataForm.id) {
            url = that.url.edit
            method = 'put'
          } else {
            url = that.url.add
            method = 'post'
          }
          httpAction(url, that.dataForm, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    close() {
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style scoped>
</style>